package org.suburban.resource;
import java.util.logging.Logger;
import java.util.logging.Level;
import com.google.appengine.api.utils.SystemProperty;
import com.google.appengine.api.xmpp.JID;
/**
 * class to predefine commonly used JIDs and
 * operations over JIDs
 * @author Alexey Palamarchuk (slonnik@gmail.com)
 *
 */
public class JIDs {
	/** logger*/
	private static final Logger logger = Logger.getLogger(JIDs.class.getName());
	/** main application id*/
	private static final String appId = SystemProperty.applicationId.get();
	/** main application domain*/
	private static final String appDomain = "@appspot.com";
	/** chat domain*/
	private static final String chatDomain = "@" +appId + ".appspotchat.com";
	/** admin id*/
	private final static String adminId = appId + appDomain;
	/** reference to admin JID*/
	public final static JID  ADMIN = new JID(adminId);
	
	/**
	 * check if incoming JID is under "admin" role
	 * @param jid - incoming JID
	 * @return - true, if incoming JID is under "admin" role 
	 */
	public static boolean isAdminJid(JID jid)
	{
		String id = normalise(jid).getId();
		boolean isAdmin = adminId.equalsIgnoreCase(id); 
		/** trace output*/
		if(logger.isLoggable(Level.FINE))
		{
			logger.log(Level.FINE, "Check incoming jid for admin role: admin jid={0} incoming jid={1} isAdmin={2}",
					new Object[]{adminId, id, isAdmin});
		}
		return isAdmin;
	}
	
	/**
	 * check if JID is under "client" role
	 * @param jid - JID to be checked
	 * @return true if JID is under "client" role, otherwise false
	 */
	public static boolean isClientJid(JID jid)
	{
		/** TODO: Implement true "check client JID" logic
		 *  At least we have to check if incoming jid, has ever been subscribed
		 */
		return true;
	}
	
	/**
	 * create new client jid
	 * @param client - client id/name
	 * @return client jid
	 */
	public static JID newClientJid(String client)
	{
		return new JID(client + chatDomain);
	}

	/**
	 * normalise incoming JID, in other words remove "/<...>"
	 * in the JID id
	 * @param jid	- incoming JID to be normalised
	 * @return - normalised jid
	 */
	public static JID normalise(JID jid)
	{
		return new JID(jid.getId().split("/")[0]);
	}
}
